<?php

class AssociationDefinition implements IAssociationDefinition
{

  public $associationForeignKey;

  public $associationPrimaryKey;

  public $associationTable;

  public $daoClass;

  public $dao;

  public $foreignKey;

  public $foreignKeyField;

  public $name;

  public $primaryKey;

  public $primaryKeyField;

  public $proxyClass;

  public $proxy;

  public $required;

  public $single;

  public $source;

  public $target;

  /**
   *
   * @param string $name
   * @return void
   */
  public function __construct($name)
  {
    $this->name = $name;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/domain/api/IAssociationDefinition#getName()
   */
  public function getName()
  {
    return $this->name;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/domain/api/IAssociationDefinition#getAssociationForeignKey()
   */
  public function getAssociationForeignKey()
  {
    if (empty($this->associationForeignKey)) {
      $this->associationForeignKey = $this->getForeignKeyField()->getName();
    }

    return $this->associationForeignKey;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/domain/api/IAssociationDefinition#getAssociationPrimaryKey()
   */
  public function getAssociationPrimaryKey()
  {
    if (empty($this->associationPrimaryKey)) {
      $this->associationPrimaryKey = $this->getPrimaryKeyField()->getName();
    }

    return $this->associationPrimaryKey;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/domain/api/IAssociationDefinition#getAssociationTable()
   */
  public function getAssociationTable()
  {
    if (empty($this->associationTable)) {
      $tables = array($this->getPrimaryKeyField()->getTable(), $this->getForeignKeyField()->getTable());

      sort($tables);

      $this->associationTable = implode('_', $tables);
    }

    return $this->associationTable;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/domain/api/IAssociationDefinition#getForeignKey()
   */
  public function getForeignKey()
  {
    return $this->foreignKey;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/domain/api/IAssociationDefinition#getForeignKeyField()
   */
  public function getForeignKeyField()
  {
    return $this->foreignKeyField;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/domain/api/IAssociationDefinition#getPrimaryKey()
   */
  public function getPrimaryKey()
  {
    return $this->primaryKey;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/domain/api/IAssociationDefinition#getPrimaryKeyField()
   */
  public function getPrimaryKeyField()
  {
    return $this->primaryKeyField;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/domain/api/IAssociationDefinition#getProxy()
   */
  public function getProxy()
  {
    if (empty($this->proxy)) {
      $class = $this->proxyClass;
      $this->proxy = new $class($this);
    }

    return $this->proxy;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/domain/api/IAssociationDefinition#getDAO()
   */
  public function getDAO()
  {
    if (empty($this->dao)) {
      $class = $this->proxyClass . 'DAO';
      $this->dao = new $class($this);
    }

    return $this->dao;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/domain/api/IAssociationDefinition#getSource()
   */
  public function getSource()
  {
    return $this->source;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/domain/api/IAssociationDefinition#getTarget()
   */
  public function getTarget()
  {
    return $this->target;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/domain/api/IAssociationDefinition#isRequired()
   */
  public function isRequired()
  {
    return $this->required;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/domain/api/IAssociationDefinition#isSingle()
   */
  public function isSingle()
  {
    return $this->single;
  }

}

?>